from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from meet001.meetup.models import Group
from meet001.meetup.location import location_code, city_district


def search_group(request):
  if request.method != "GET":
    return
  group_name = request.GET['group_name']
  city = request.GET['city']
  district = request.GET['district']
  matched_groups = None
  search_keyword = None
  if city == 'others' or district == 'others':
    matched_groups = Group.objects.filter(name__icontains=group_name)
    search_keywords = '' + group_name + '' 
  else:
    matched_groups = Group.objects.filter(name__icontains=group_name,
        location=location_code[(city, district)])
    search_keywords = '' + group_name + ' in ' + city + ' and ' + district + '' 
  return render_to_response('group_search_result.html',
      {'matched_group_num':len(matched_groups),
        'search_keywords':search_keywords,
        'matched_groups':matched_groups})
